<?php
// 应用公共文件
function curl_post($url, $keysArr=array(), $flag = 0,$headers=array(),$timeout = 60){
	$ch = curl_init();
	if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_POST, TRUE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($ch, CURLOPT_URL, $url);
    // 设置请求的最长执行时间为 30 秒
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	if($headers){
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	}
	try {
		$response = curl_exec($ch);
	} catch (\Exception $e) {
       return null;
    }
	$curlError = curl_error($ch);
    if(!empty($curlError)) {
       return null;
    }
	curl_close($ch);
	//-------请求为空
	if(empty($response)){
		return null;
	}
	return $response;
}
function curl_get($url,$keysArr=array(),$headers=array(),$timeout=60){
	if(!empty($keysArr)){
		$url = strpos($url,'?')===false ? ($url."?") : ($url."&");
		$valueArr = array();
		foreach($keysArr as $key => $val){
			$valueArr[] = "$key=$val";
		}
		$keyStr = implode("&",$valueArr);
		$url .= ($keyStr);
	}

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    // 设置请求的最长执行时间为 30 秒
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	if($headers){
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	}
	try {
		$response = curl_exec($ch);
	} catch (\Exception $e) {
       return null;
    }
	$curlError = curl_error($ch);
    if(!empty($curlError)) {
       return null;
    }
	curl_close($ch);
	//-------请求为空
	if(empty($response)){
		return null;
	}
	return $response;
}

 function getSign(){
    $params = request()->post();
    unset($params['sign']);
    ksort($params);
    foreach( $params AS $key => $v ){
        if ( $v == '' ){
            unset($params[$key]);
        }
    }
    $signStr = http_build_query($params) . config('app.secretkey');
    $sign = md5($signStr);
    //echo $sign;
    return $sign;
}